<?php

namespace common\models;

use Yii;
use yii\helpers\Html;
/**
 * This is the model class for table "doc".
 *
 * @property int $did
 * @property int $cid
 * @property string $title
 * @property string $keywords
 * @property string $summary
 * @property string $content
 * @property string $authorname
 * @property int $time
 * @property int $lastedit
 * @property string $lasteditorname
 * @property int $viewnum
 * @property string $hisedition
 * @property int $status
 * @property int $locked
 */
class Doc extends \yii\db\ActiveRecord
{
	private $_oldTags;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'doc';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['content', ], 'required'],
			[['cid', 'title', 'keywords', 'summary', 'content', 'authorname', 'lasteditorname',  'status'], 'safe'],
            [['cid', 'time', 'lastedit','viewnum', 'status', 'locked'], 'integer'],
            [['keywords', 'summary', 'content', 'hisedition'], 'string'],
            [['title', 'authorname', 'lasteditorname'], 'string', 'max' => 128],
		
		];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'did' => 'Did',
            'cid' => 'Cid',
            'title' => '标题',
            'keywords' => '关键词',
            'summary' => '摘要',
            'content' => '内容',
            'authorname' => '创建者',
            'time' => '创建时间',
            'lastedit' => '修改时间',
            'lasteditorname' => '修改者',
            'viewnum' => '浏览次数',
            'hisedition' => '历史版本',
            'status' => '词条状态',
            'locked' => '是否锁定',
        ];
    }
 public function afterFind()
    {
    	parent::afterFind();
    	$this->_oldTags = $this->keywords;
    }
    
    public function afterSave($insert, $changedAttributes)
    {
    	parent::afterSave($insert, $changedAttributes);
    	Word::updateNum($this->_oldTags, $this->keywords);
    }
    
    public function afterDelete()
    {
    	parent::afterDelete();
    	Word::updateNum($this->keywords, '');
    }
    

	  public function getBeginning($length=50)
    {
    	$tmpStr = strip_tags($this->content);
    	$tmpLen = mb_strlen($tmpStr);
    	 
    	$tmpStr = mb_substr($tmpStr,0,$length,'utf-8');
    	return $tmpStr.($tmpLen>$length?'...':'');
    }

    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['cid' => 'cid']);
    }	
    public function getStatus0()
    {
        return $this->hasOne(Docstatus::className(), ['id' => 'status']);
    }
    public function getLockstatus0()
    {
        return $this->hasOne(Lockstatus::className(), ['id' => 'locked']);
    }	
  
	   public function getUrl()
    {
    	return Yii::$app->urlManager->createUrl(
    			['doc/detail','did'=>$this->did,'title'=>$this->title]);
    }
	    public function  getKeywordLinks()
    {
    	$links=array();
    	foreach(Word::string2array($this->keywords) as $word)
    	{
    		$links[]=Html::a(Html::encode($word),array('doc/index','DocSearch[keywords]'=>$word));
    	}
    	return $links;
    }
	    public static function findRecentDocs($limit=10)
    {
    	return Doc::find()->where(['locked'=>1])->orderBy('time DESC')
    	->limit($limit)->all();
    } 
	public static function findHotDocs($limit=10)
    {
    	return Doc::find()->where(['locked'=>1,'status'=>2])
    	->limit($limit)->all();
    }
     public function approve()
    {
    	$this->status = 2; //设置词条状态为已审核
    	return ($this->save()?true:false);
    } 
     public function lock()
    {
    	$this->locked = 2; //设置词条状态为已审核
    	return ($this->save()?true:false);
    } 		
    public static function getPengdingDocCount()
    {
    	return Doc::find()->where(['status'=>1])->count();
    }
    public function  getWordLinks()
    {
    	$links=array();
    	foreach(Word::string2array($this->keywords) as $word)
    	{
    		$links[]=Html::a(Html::encode($word),array('doc/category','DocSearch[keywords]'=>$word));
    	}
    	return $links;
    }	
}
